<?php
/**
 * @link https://gitee.com/toshcn/yii2-oauth2-server
 * @copyright Copyright (c) 2018 len168.com
 * @license https://gitee.com/toshcn/yii2-oauth2-server/blob/master/LICENSE
 */

namespace toshcn\yii2\oauth2server\models;

use Yii;

/**
 * This is the model class for table "{{%oauth_authorization_codes}}".
 *
 * @property string $authorization_code
 * @property string $client_id
 * @property string $user_id
 * @property string $redirect_uri
 * @property string $expires
 * @property string $scope
 *
 * @property OauthClients $client
 */
class OauthAuthorizationCodes extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%oauth_authorization_codes}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['authorization_code', 'client_id', 'user_id', 'redirect_uri', 'scope'], 'required'],
            [['user_id'], 'integer'],
            [['expires'], 'safe'],
            [['authorization_code', 'client_id'], 'string', 'max' => 80],
            [['redirect_uri', 'scope'], 'string', 'max' => 2000],
            [['authorization_code'], 'unique'],
            [['client_id'], 'exist', 'skipOnError' => true, 'targetClass' => OauthClients::className(), 'targetAttribute' => ['client_id' => 'client_id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'authorization_code' => Yii::t('OauthAuthorizationCodes', 'Authorization Code'),
            'client_id' => Yii::t('OauthAuthorizationCodes', 'Client ID'),
            'user_id' => Yii::t('OauthAuthorizationCodes', 'User ID'),
            'redirect_uri' => Yii::t('OauthAuthorizationCodes', 'Redirect Uri'),
            'expires' => Yii::t('OauthAuthorizationCodes', 'Expires'),
            'scope' => Yii::t('OauthAuthorizationCodes', 'Scope'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getClient()
    {
        return $this->hasOne(OauthClients::className(), ['client_id' => 'client_id']);
    }
}
